/**
	Copyright (c) 2011 ATTi 
	All rights reserved.
	Author: Michael Shu
	Purpose : Copy from owner's info into case fields.
	Project : CCIR, CCOOR
	Date: 1/21/2011
*/

trigger CaseFieldsUpdateFromOwnerInfoTrigger on Case (before insert, before update) {
	if(trigger.isBefore){
		set<ID> crtRanges = CaseFieldsUpdateFromOwnerHelper.getCaseRTIds();
		list<Case> cases = new list<Case>();
		if(trigger.isInsert){
			for(Case c : trigger.new){
				if(crtRanges.contains(c.RecordTypeId)){
					cases.add(c);
				}
				CaseFieldsUpdateFromOwnerHelper.setPrevOwnerAndLastQueueOwner(c, null);
			}
		}else if(trigger.isUpdate){
			for(Case c : trigger.new){
				if(crtRanges.contains(c.RecordTypeId)){
					Case oldcs = trigger.oldMap.get(c.Id);
					if(oldcs.OwnerId != c.OwnerId){
						cases.add(c);
					}
					CaseFieldsUpdateFromOwnerHelper.setPrevOwnerAndLastQueueOwner(c, oldcs);
				}
			}			
		}
		if(!cases.isEmpty()){
			CaseFieldsUpdateFromOwnerHelper.setWhenCaseOwnerChange(cases);
		}
	}	
}